package znbkListXtfx.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import znbkListXtfx.entity.SystemReviewListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\1\29 0029.
 */

public class SystemReviewListAdapter extends BaseRecyclerAdapter<SystemReviewListAdapter.ViewHolder> {

    private Context mContext;
    private List<SystemReviewListEntity.ModelPaperInfoListBean> mList;
    private int mLastPosition = -1;
    private OnItemClickListener onItemClickListener;

    public SystemReviewListAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
    }

    public SystemReviewListAdapter(Context mContext, List<SystemReviewListEntity.ModelPaperInfoListBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public  interface OnItemClickListener{
        void onItemClick(View view, List<SystemReviewListEntity.ModelPaperInfoListBean> list, int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_xtfx, parent,false);
        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,  int position, boolean isItem) {
        holder.rippleLayout.setTag(position);
        //设置波纹效果
        holder.rippleLayout.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {
                if ( mList.size() != 0) {
                    if (onItemClickListener != null){
                        onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
                    }
                }
            }
        });


        SystemReviewListEntity.ModelPaperInfoListBean itemBean = mList.get(position);

        if(itemBean.getSubjectID().equals("C")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_english);
        }else if(itemBean.getSubjectID().equals("A")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_chinese);
        }else if(itemBean.getSubjectID().equals("D")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_physical);
        }else if(itemBean.getSubjectID().equals("E")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_chemical);
        }else if(itemBean.getSubjectID().equals("F")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_biologcial);
        }else if(itemBean.getSubjectID().equals("H")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_history);
        }else if(itemBean.getSubjectID().equals("I")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_geographic);
        }else if(itemBean.getSubjectID().equals("B")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_mathematics);
        }else if(itemBean.getSubjectID().equals("G")){
            holder.paperImageCover.setBackgroundResource(R.mipmap.znbk_cover_political);
        }


        if (itemBean.getAnswerState() == 3){//已经提交
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.znbk_icon_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("查看结果");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getAnswerState() == 2){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.znbk_icon_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("正在自评");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getAnswerState() == 1){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.znbk_icon_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("继续作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getAnswerState() == 0){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.znbk_icon_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("开始作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }
        int finishedCount = itemBean.getAnsweredItemCount();
        int totalCount = itemBean.getTotalItemCount();
        String paperTotal = String.format(mContext.getString(R.string.paper_itemTotal),totalCount);
        String paperFinishedTotal = String.format(mContext.getString(R.string.paper_itemFinishedTotal),finishedCount);


        holder.paperInduction.setText(itemBean.getPaperName());
        holder.paperTotal.setText(Html.fromHtml(paperTotal));
        holder.paperFinishedTotal.setText(Html.fromHtml(paperFinishedTotal));

        /*
        * 显示进度百分比
        * */
        float finishedPercent = (float) Math.floor((float)finishedCount / totalCount * 100) / 100;
        Log.e("=finishedPercent==","==="+finishedPercent);
        holder.progressBar.setProgress((int)(finishedPercent*100));
        if(holder.progressBar.getProgress()==0){
            holder.progressBar.setVisibility(View.INVISIBLE);
        }else{
            holder.progressBar.setVisibility(View.VISIBLE);
        }


        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }
        mLastPosition = adapterPosition;

    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    public SystemReviewListEntity.ModelPaperInfoListBean getItem(int position) {
        return mList.get(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView paperImageCover;//资料封面图片
        private TextView paperLastAnswerTime;
        private  TextView paperFinishedTotal;
        private RippleView rippleLayout;
        private TextView paperInduction;//资料简介
        private TextView paperTotal;//资料试题数量
        private ProgressBar progressBar;//资料进度显示
        private LinearLayout paperInto;
        private TextView tvIntoDetail;

        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                //波纹效果
                rippleLayout = (RippleView) itemView.findViewById(R.id.item_ripper);
                paperImageCover = (ImageView) itemView.findViewById(R.id.img_paper);
                paperInduction = (TextView) itemView.findViewById(R.id.tv_induction);
                paperLastAnswerTime = (TextView) itemView.findViewById(R.id.tv_lastTime);
                paperTotal = (TextView) itemView.findViewById(R.id.tv_total);
                paperFinishedTotal = (TextView) itemView.findViewById(R.id.tv_answered);
                progressBar = (ProgressBar) itemView.findViewById(R.id.pb_finished);
                paperInto = (LinearLayout) itemView.findViewById(R.id.liner_into);
                tvIntoDetail = (TextView) itemView.findViewById(R.id.tv_into_detail);
            }
        }
    }


    public void clearAndAdd(List<SystemReviewListEntity.ModelPaperInfoListBean> data){
        mLastPosition = -1;
        mList.clear();
        mList.addAll(data);
        notifyDataSetChanged();
    }
    public void add(List<SystemReviewListEntity.ModelPaperInfoListBean> data){
        int startPosition = getStart()+getAdapterItemCount();
        mList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());

    }
}
